package com.service.devimonitor;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bpms.common.CommonUtil;
import com.bpms.common.JdbcWorker;
import com.bpms.common.VO;
import com.bpms.common.VOList;
import com.bpms.esb.IService;
import com.zk.atm.forms.AtmpDeviceStatusForm;
import com.zk.atm.forms.AtmpOrgForm;
import com.zk.atm.utils.AccessDBConnection;
import com.zk.atm.utils.ConstantsAtmp;
import com.zk.atm.utils.ConstantsAtmpSql;
import com.zk.atm.utils.JsonUtil;
import com.zk.atm.utils.PrimaryKeyUtil;
import com.zk.atm.utils.StringUtil;
import com.zk.atm.vo.AtmpOrg;
import com.zk.atm.vo.BaseVo;

public class Atmpmanagerserviceimpl implements IService {

	protected Logger logger=Logger.getLogger(Atmpmanagerserviceimpl.class);
	@Override
	public VO exec(VO args) {
		VO r=new VO();
		String cmd="";
		try{
			cmd=StringUtil.getPropertyValue(args.getAttr(ConstantsAtmp.ATMP_CMD_METHOD_KEY_NAME));
			if(cmd.equals(ConstantsAtmp.ATMP_MANAGER_LIST)){
				VO in_clude_list=this.list(args);
				return in_clude_list;
			}else if(cmd.equals(ConstantsAtmp.ATMP_MANAGER_SAVE)){
				VO ret=this.save(args);
				return ret;
			} else if(cmd.equals(ConstantsAtmp.ATMP_MANAGER_DELETE)){
				VO ret=this.delete(args);
				return ret;
			}else if(cmd.equals(ConstantsAtmp.ATMP_MANAGER_RESET_PWD)){
				VO ret=this.reset_pwd(args);
				return ret;
			}
			
		}catch(Exception e){
			CommonUtil.debug("atmpmanagerserviceimpl exception"+e);
		}
		return r;
	}


	public VO list(VO args)   throws Exception{
		VO result_vo=new VO();
		String username=StringUtil.getPropertyValue(args.getAttr("username"));
		String device_sql=" select *from manager m where 1=1 ";
		
//		if(!userid.equals("")){
//			AtmpDeviceStatusForm form=new AtmpDeviceStatusForm();
//			form.setUserid(userid);
//			String belong_ids=ConstantsAtmpSql.getBelongOrgIds(form);
//			if(!belong_ids.equals("")){
//			   device_sql+=" and org.org_id in ("+belong_ids+")";
//			}
//		}
		if(!username.equals("")){
			device_sql+=" and m.username like '%"+username+"%'";
		}
		device_sql+=" order by m.username ";
		JdbcWorker j=new JdbcWorker();
		VOList volist=j.readAll(new VO(), device_sql);
		if(logger.isDebugEnabled()){
		  //System.out.println("************************size="+volist.listData.size()+",volist="+volist.listData);
		}
		result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET, "0");
		result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "操作成功!");
		result_vo.setObj(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_VOLIST, volist);
		//System.out.println(JsonUtil.getJsonObjectByBpmVo(result_vo));
		return result_vo;
	}
	
	public VO save(VO form_vo) throws Exception{
		VO result_vo=new VO();
		
		try {
			String id=StringUtil.getPropertyValue(form_vo.getAttr("id"));
			String table_name="manager";
			JdbcWorker jdbc=new JdbcWorker();
			VO vo=new VO();
			String username=StringUtil.getPropertyValue(form_vo.getAttr("username"));
			vo.setAttr("username", username);
			vo.setAttr("password", form_vo.getAttr("password"));
			//把device里面的值,全部封装到bpmvo中
			String primary_column="id";
			//如果id为空,则表明是新增操作,则获取生成的主键,保存
			if(id.equals("")){
				//如果是新增操作,则判断是否有重复的ip地址
				boolean is_exists=this.checkDeviceIPIsExsitsInAtmpDevice(vo);
				if(is_exists==true){
					result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET, "1");
					result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE,
							"当前大堂经理名称:"+username+",在数据库中已经存在,请您重新输入!");
					return result_vo;
				}
				String primary_key=PrimaryKeyUtil.getPrimaryKey(table_name, null);
				vo.setAttr(primary_column, primary_key);
			    vo.voType=table_name;
			    jdbc.insert(vo);
			    System.out.println("新增操作成功!");
			}else{
				VO key_vo=new VO();
				key_vo.setAttr(primary_column, id);
				key_vo.keyString=primary_column;
				key_vo.voType=table_name;
				jdbc.update(vo, key_vo);
				System.out.println("更新操作成功!");
			}
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET, "0");
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "操作成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "操作失败!");
		}
		return result_vo;
	}
	
	protected static boolean  checkDeviceIPIsExsitsInAtmpDevice(VO vo)throws Exception {
		String username=StringUtil.getPropertyValue(vo.getAttr("username"));
		if(username.equals("")){
			return false;
		}
		String sql="      select count(*) as total from manager m where 1=1 and m.username='"+username+"'";
		JdbcWorker jdbc=null;
		try{
			jdbc=new JdbcWorker();
			VOList volist=jdbc.readAll(new VO(), sql);
			if(volist!=null&&volist.listData.size()>0){
				VO result_vo=(VO) volist.listData.get(0);
				String total=StringUtil.getPropertyValue(result_vo.getAttr("total"));
				if(!total.equals("")){
					return Integer.parseInt(total)>0?true:false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(jdbc!=null){
			 jdbc=null;
			}
		}
		return false;
	}
	
	
//	protected VO getVO(VO form_vo,BaseVo clazz,Map<String,String> filter_field_map) throws Exception{
//		VO vo=new VO();
//		Field[] fields=clazz.getClass().getDeclaredFields();
//		for (Field field : fields) {
//			String field_name=field.getName();
//			//过滤不需要的字段
//			if(filter_field_map.get(field_name)==null){
//				continue;
//			}
//			String field_value=StringUtil.getPropertyValue(form_vo.getAttr(field_name));
//			System.out.println("field_name="+field_name+",field_value="+field_value);
//			vo.setAttr(field_name,field_value);
//		}
//		return vo;
//	}
	
	public VO delete(VO form_vo) throws Exception {
		VO result_vo=new VO();
		try {
			String id=StringUtil.getPropertyValue(form_vo.getAttr("id"));
			if(id.equals("")){
				String msg="参数值:id为空,不能执行删除操作!";
				System.out.println(msg);
				logger.error("delete:atmp_org:"+msg);
				result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, msg);
				return result_vo;
			}
			
//			boolean is_refer=this.checkOrgReferAtmpDevice(form_vo);
//			if(is_refer==true){
//				String msg="当前要删除的ID为"+id+"的机构,在设备表中有记录,不能执行删除操作!";
//				System.out.println(msg);
//				logger.error("delete:atmp_org:"+msg);
//				result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, msg);
//				return result_vo;
//			}
			
			String exec_sql="  delete from manager m  where 1=1 " +
					" and m.id='"+id+"'";
			AtmpOrgForm form=new AtmpOrgForm();
			form.setExec_sql(exec_sql);
			AtmpStatusCurrentReflect.executeSaveOrDelBySql(form);
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET, "0");
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "操作成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "操作失败!");
		}
		return result_vo;
	}
	
	public VO reset_pwd(VO form_vo) throws Exception{
		VO result_vo=new VO();
		JdbcWorker jdbc=null;
		try {
			String id=StringUtil.getPropertyValue(form_vo.getAttr("id"));
			if(id.equals("")){
				result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "主键参数值为空,操作失败!");
			    return result_vo;
			}
			String table_name="manager";
			jdbc=new JdbcWorker();
			VO vo=new VO();
			//把device里面的值,全部封装到bpmvo中
			String primary_column="id";
			vo.setAttr("id", id);
			vo.setAttr("password", "888888");
			//如果id为空,则表明是新增操作,则获取生成的主键,保存
			
			VO key_vo=new VO();
			key_vo.setAttr(primary_column, id);
			key_vo.keyString=primary_column;
			key_vo.voType=table_name;
			jdbc.update(vo, key_vo);
			System.out.println("重置密码操作成功!");
			
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_RET, "0");
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "操作成功!");
		} catch (Exception e) {
			e.printStackTrace();
			result_vo.setAttr(ConstantsAtmp.ATMP_GATEWAYSERVLET_CONVERT_BPMVO_TO_JSON_MESSAGE, "操作失败!");
		} finally{
			if(jdbc!=null){
			  jdbc=null;
			}
		}
		return result_vo;
	}
}
